#include "suffer.h"

#include <QDebug>

Suffer::Suffer(QWidget *parent)
    : QDialog(parent)
{
    resize(sufferWidth, sufferHeight);
    setStyleSheet("Suffer{background-color:black}");
    qsrand(quint32(time(nullptr)));

    labelGameOver = new QLabel(this);
    labelGameOver->resize(200, 30);
    labelGameOver->setStyleSheet("QLabel{background-color:black;color:white}");
    labelGameOver->setText(tr("%1    %2").arg(words[num]).arg(num));
    labelGameOver->move((sufferWidth - 200) / 2, 20);

    for (int i = 0; i < 50; i ++ )
    {
        hello[i] = new Obstacle(this);
        hello[i]->helloW = new QLabel(this);
    }
    playerModel = new PlayerModel(this);
    playerModel->player = new QLabel(this);
    playerModel->player->move(playerModel->x, playerModel->y);
    playerModel->player->resize(playerModel->width, playerModel->height);
    playerModel->player->setStyleSheet("QLabel{background-color:red}");

    label_1 = new QLabel(this);
    label_1->resize(400, 30);
    label_1->setText(tr("按下空格试试？"));
    label_1->move((sufferWidth - 100) / 2, sufferHeight / 2);
    label_1->setStyleSheet("QLabel{background-color:black;color:white}");

    timer_1 = new QTimer(this);
    connect(timer_1, &QTimer::timeout, this, &Suffer::playerMove);
    timer_1->start(30);
    timer_2 = new QTimer(this);
    connect(timer_2, &QTimer::timeout, this, &Suffer::createHello);
    timer_2->start(3000);


}

Suffer::~Suffer()
{
}

void Suffer::playerMove()
{
    labelGameOver->setText(tr("%1      %2").arg(words[num % 38]).arg(num));
    for (int i = 0; i < num; i ++ )
    {
        if (playerModel->x >= hello[i]->x && playerModel->x <= hello[i]->x + hello[i]->size &&
                playerModel->y >= hello[i]->y && playerModel->y <= hello[i]->y + hello[i]->size)
            gameOver();

        switch (qrand()%5)
        {
        case 0:
            break;
        case 1:
            hello[i]->moveUp(78);
            break;
        case 2:
            hello[i]->moveRight(sufferWidth - 87);
            break;
        case 3:
            hello[i]->moveDown(sufferHeight - 87);
            break;
        case 4:
            hello[i]->moveLeft(78);
            break;
        }
    }

    switch (playerModel->gravity)
    {
    case 0:
        break;
    case 1:
        playerModel->moveUp(78);
//        if (playerModel->keyDown || playerModel->jumping == 1) playerModel->jump(1);
        break;
    case 2:
        playerModel->moveRight(sufferWidth - 87);
//        if (playerModel->keyLeft || playerModel->jumping == 2) playerModel->jump(2);
        break;
    case 3:
        playerModel->moveDown(sufferHeight - 87);
//        if (playerModel->keyUp || playerModel->jumping == 3) playerModel->jump(3);
        break;
    case 4:
        playerModel->moveLeft(78);
//        if (playerModel->keyRight || playerModel->jumping == 4) playerModel->jump(4);
        break;
    }


    if (playerModel->keyUp && playerModel->gravity != 1 && playerModel->gravity != 3) playerModel->moveUp(78);
    if (playerModel->keyRight && playerModel->gravity != 2 && playerModel->gravity != 4) playerModel->moveRight(sufferWidth - 87);
    if (playerModel->keyDown && playerModel->gravity != 1 && playerModel->gravity != 3) playerModel->moveDown(sufferHeight - 87);
    if (playerModel->keyLeft && playerModel->gravity != 2 && playerModel->gravity != 4) playerModel->moveLeft(78);
}

void Suffer::createHello()
{
    playerModel->gravity = (playerModel->gravity + 1) % 5;
    hello[num]->x = qrand()%225 + 88;
    hello[num]->y = qrand()%225 + 88;
    hello[num]->helloW->resize(hello[num]->size, hello[num]->size);
    hello[num]->helloW->move(hello[num]->x, hello[num]->y);
    hello[num]->helloW->setStyleSheet("QLabel{background-color:rgb(255,255,255)}");
    qDebug() << num << hello[num]->x << hello[num]->y;
    num ++ ;
}

void Suffer::gameOver()
{
    timer_1->stop();
    timer_2->stop();
    labelGameOver->setText(tr("你是谁"));
}

void Suffer::paintEvent(QPaintEvent *event)
{
   QPainter painter(this);
   painter.setPen(QPen(Qt::white, 10));
   painter.drawRect(QRect(70, 70, sufferWidth - 140, sufferHeight - 140));
   painter.end();
}

void Suffer::keyPressEvent(QKeyEvent *event)
{
    label_1->hide();
//    qDebug() << playerModel->gravity << playerModel->jumping << playerModel->jumpSpeed;
//    if (playerModel->gravity && !playerModel->jumping)
//    {
//        if (event->key() == Qt::Key_Up && !playerModel->keyUp) playerModel->jumping = 3;
//        if (event->key() == Qt::Key_Right && !playerModel->keyRight) playerModel->jumping = 2;
//        if (event->key() == Qt::Key_Down && !playerModel->keyDown) playerModel->jumping = 1;
//        if (event->key() == Qt::Key_Left && !playerModel->keyLeft) playerModel->jumping = 4;
//    }

    if (event->key() == Qt::Key_Space) playerModel->gravity = (playerModel->gravity + 1) % 5;
    if (event->key() == Qt::Key_Up && !playerModel->keyUp) playerModel->keyUp = true;
    if (event->key() == Qt::Key_Right && !playerModel->keyRight) playerModel->keyRight = true;
    if (event->key() == Qt::Key_Down && !playerModel->keyDown) playerModel->keyDown = true;
    if (event->key() == Qt::Key_Left && !playerModel->keyLeft) playerModel->keyLeft = true;


}

void Suffer::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Up) playerModel->keyUp = false;
    if (event->key() == Qt::Key_Right) playerModel->keyRight = false;
    if (event->key() == Qt::Key_Down) playerModel->keyDown = false;
    if (event->key() == Qt::Key_Left) playerModel->keyLeft = false;

}

void Suffer::closeEvent(QCloseEvent *event)
{
    timer_1->stop();
    timer_2->stop();
    event->accept();
}
